package cn.com.infohold.smartcity.activity.Dispute;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.DisputeBean;
import cn.com.infohold.smartcity.api.bean.LandInfoBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class DisputeAdd_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.ll_cb_warrant)
    LinearLayout ll_cb_warrant;
    @ViewInject(R.id.ll_cb_land)
    LinearLayout ll_cb_land;
    @ViewInject(R.id.ll_cb_contract)
    LinearLayout ll_cb_contract;

    @ViewInject(R.id.cb_warrant)
    CheckBox cb_warrant;
    @ViewInject(R.id.cb_land)
    CheckBox cb_land;
    @ViewInject(R.id.cb_contract)
    CheckBox cb_contract;

    @ViewInject(R.id.et_code)
    EditText et_code;
    @ViewInject(R.id.et_content)
    EditText et_content;
    @ViewInject(R.id.btn_commit)
    Button btn_commit;

    private DisputeBean disputeBean;
    private String disputeType = "";
    private String code = "";

    @Override
    protected Object inflateView() {
        return R.layout.activity_add_dispute;
    }

    @Override
    protected void initView(View anchor) {

        disputeBean = new DisputeBean();

        ll_cb_warrant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_warrant.setChecked(true);
                cb_land.setChecked(false);
                cb_contract.setChecked(false);
                disputeType = "1";
            }
        });

        ll_cb_land.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_land.setChecked(true);
                cb_warrant.setChecked(false);
                cb_contract.setChecked(false);
                disputeType = "2";
            }
        });

        ll_cb_contract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_contract.setChecked(true);
                cb_warrant.setChecked(false);
                cb_land.setChecked(false);
                disputeType = "3";
            }
        });

        btn_commit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (disputeType.equals("")){
                    Toast.makeText(DisputeAdd_Activity.this, "请选择纠纷类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (et_code.getText().toString().equals("")){
                    Toast.makeText(DisputeAdd_Activity.this, "请输入编号", Toast.LENGTH_SHORT).show();
                    return;
                }
                code = et_code.getText().toString();
                checkExist();
            }
        });
    }

    private void checkExist(){
        SDK.api().checkDisputeExist(disputeType,code).postEvent(RequestCode.REQUEST_CODE_CHECK_DISPUTE_EXIST);
    }

    private void initData(){
        SDK.api().addDispute(disputeBean).postEvent(RequestCode.REQUEST_CODE_ADD_DISPUTE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent) {

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_CHECK_DISPUTE_EXIST
                && requestCode != RequestCode.REQUEST_CODE_ADD_DISPUTE) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode) {
                case RequestCode.REQUEST_CODE_CHECK_DISPUTE_EXIST:
                    ProgressDialog.dismiss();
                    Type type = new TypeToken<LandResult<List<LandInfoBean>>>() {
                    }.getType();
                    LandResult<List<LandInfoBean>> result = GsonUtil.fromJson(httpEvent.getData(), type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result.getStatus().equals("1")){
                        switch (disputeType){
                            case "1":
                                disputeBean.setWarrantId(et_code.getText().toString());
                                break;
                            case "2":
                                disputeBean.setLandId(et_code.getText().toString());
                                break;
                            case "3":
                                disputeBean.setContractnumber(et_code.getText().toString());
                                break;
                            default:
                                break;
                        }
                        disputeBean.setDisputeContent(et_content.getText().toString());
                        disputeBean.setUserid(XmlData.getInstance().getUserInfo().getId());
                        initData();
                    }

                    break;
                case RequestCode.REQUEST_CODE_ADD_DISPUTE:
                    Type type1 = new TypeToken<LandResult<Map<String,String>>>(){}.getType();
                    LandResult<Map<String,String>> result1 = GsonUtil.fromJson(httpEvent.getData(),type1);
                    Toast.makeText(this, result1.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result1.getStatus().equals("1")){
                        finish();
                        overridePendingTransitionFade();
                    }

                    break;

                default:
                    break;
            }
        }
    }

}
